<?php

//短信验证中间件

namespace app\api\middleware;
use support\exception\ValidateException;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use think\facade\Cache;


class SmsAuth implements MiddlewareInterface
{
	
	
    public function process(Request $request, callable $handler) : Response
	{
		$verify_id = $request->post('verify_id');
		$verify	= $request->post('verify');
		
		$mobile	= $request->post('mobile');
		if(empty($verify_id) || empty($verify)){
			return json(['status'=>411,'msg'=>'短信验证ID或者验证码不能为空']);
		}
		$cacheData = Cache::get($verify_id);
		
		if(!$cacheData){
			return json(['status'=>411,'msg'=>'验证码已过期']);
		}
		
		if($cacheData['code'] <> $verify){
			return json(['status'=>411,'msg'=>'验证码错误']);
		}
		
		if($cacheData['mobile'] <> $mobile){
			return json(['status'=>411,'msg'=>'手机号与验证不一致']);
		}
		
		return $handler($request);
    }
} 